package com.ruoyi.customerSample.service.impl;

import com.ruoyi.common.utils.uuid.IdUtils;

import com.ruoyi.customerSample.domain.SampleCirculationBill;
import com.ruoyi.customerSample.domain.SampleCirculationDetail;
import com.ruoyi.customerSample.domain.SampleReceptionBill;
import com.ruoyi.customerSample.mapper.SampleCirculationBillMapper;
import com.ruoyi.customerSample.mapper.SampleCirculationDetailMapper;
import com.ruoyi.customerSample.mapper.SampleReceptionBillMapper;
import com.ruoyi.customerSample.service.ISampleCirculationBillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 样品流转记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-05-05
 */
@Service
public class SampleCirculationBillServiceImpl implements ISampleCirculationBillService
{
    @Autowired(required = false)
    private SampleCirculationBillMapper sampleCirculationBillMapper;

    @Autowired(required = false)
    private SampleCirculationDetailMapper sampleCirculationDetailMapper;

    @Autowired(required = false)
    private SampleReceptionBillMapper sampleReceptionBillMapper;

    /**
     * 查询样品流转记录
     * 
     * @param id 样品流转记录ID
     * @return 样品流转记录
     */
    @Override
    public SampleCirculationBill selectSampleCirculationBillById(String id)
    {
        SampleCirculationBill bill = sampleCirculationBillMapper.selectSampleCirculationBillById(id);
        //查询列表数据
        SampleCirculationDetail details = new SampleCirculationDetail();
        details.setCirculationId(id);
        List<SampleCirculationDetail> list = sampleCirculationDetailMapper.selectSampleCirculationDetailList(details);
        if(list.size() > 0) {
            bill.setList(list);
        }
        return bill;
    }

    /**
     * 查询样品流转记录列表
     * 
     * @param sampleCirculationBill 样品流转记录
     * @return 样品流转记录
     */
    @Override
    public List<SampleCirculationBill> selectSampleCirculationBillList(SampleCirculationBill sampleCirculationBill)
    {
        List<SampleCirculationBill> list = sampleCirculationBillMapper.selectSampleCirculationBillList(sampleCirculationBill);
        if(list.size() > 0) {
            for(SampleCirculationBill bill : list) {
                //查看详情信息
                SampleCirculationDetail sampleDetailBill = new SampleCirculationDetail();
                sampleDetailBill.setCirculationId(bill.getId());
                List<SampleCirculationDetail> slist = sampleCirculationDetailMapper.selectSampleCirculationDetailList(sampleDetailBill);
                if(slist.size() > 0) {
                    bill.setList(slist);
                }
            }
        }
        return list;
    }

    /**
     * 新增样品流转记录
     * 
     * @param sampleCirculationBill 样品流转记录
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertSampleCirculationBill(SampleCirculationBill sampleCirculationBill)
    {
        String id = IdUtils.fastSimpleUUID();
        //增加详情信息
        if(sampleCirculationBill.getList().size() > 0) {
            for(SampleCirculationDetail detailBill : sampleCirculationBill.getList()) {
                detailBill.setId(IdUtils.fastSimpleUUID());
                detailBill.setCirculationId(id);
            }
            sampleCirculationDetailMapper.batchSampleCirculationDetail(sampleCirculationBill.getList());
        }
        sampleCirculationBill.setId(id);
        sampleCirculationBill.setState("2");
        int ret = sampleCirculationBillMapper.insertSampleCirculationBill(sampleCirculationBill);
        if(ret > 0) {
            //更新样品接收单状态
            SampleReceptionBill sampleReceptionBill = new SampleReceptionBill();
            sampleReceptionBill.setState("2");
            sampleReceptionBill.setItemNum(sampleCirculationBill.getItemNum());
            sampleReceptionBillMapper.updateSampleReceptionBillByNum(sampleReceptionBill);
        }
        return ret;
    }

    /**
     * 修改样品流转记录
     * 
     * @param sampleCirculationBill 样品流转记录
     * @return 结果
     */
    @Override
    public int updateSampleCirculationBill(SampleCirculationBill sampleCirculationBill)
    {
        int ret = sampleCirculationBillMapper.updateSampleCirculationBill(sampleCirculationBill);
        if(ret > 0) {
            //删除原本信息
            sampleCirculationDetailMapper.deleteSampleDetailBillByCid(sampleCirculationBill.getId());
            //增加新的数据
            if(sampleCirculationBill.getList().size() > 0) {
                for(SampleCirculationDetail detailBill : sampleCirculationBill.getList()) {
                    detailBill.setId(IdUtils.fastSimpleUUID());
                    detailBill.setCirculationId(sampleCirculationBill.getId());
                }
                sampleCirculationDetailMapper.batchSampleCirculationDetail(sampleCirculationBill.getList());
            }
        }
        return ret;
    }

    /**
     * 批量删除样品流转记录
     * 
     * @param ids 需要删除的样品流转记录ID
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteSampleCirculationBillByIds(String[] ids)
    {
        for(int i = 0; i < ids.length; i++) {
            SampleCirculationBill bills = sampleCirculationBillMapper.selectSampleCirculationBillById(ids[i]);
            //变更登记单状态
            SampleReceptionBill sampleReceptionBill = new SampleReceptionBill();
            sampleReceptionBill.setItemNum(bills.getItemNum());
            sampleReceptionBill.setState("1");
            sampleReceptionBillMapper.updateSampleReceptionBillByNum(sampleReceptionBill);
        }
        int ret = sampleCirculationBillMapper.deleteSampleCirculationBillByIds(ids);
        if(ret > 0) {
            //删除明细单据
            if(ids.length > 0) {
                for(int i = 0; i < ids.length; i++) {
                    sampleCirculationDetailMapper.deleteSampleDetailBillByCid(ids[i]);
                }
            }
        }
        return ret;
    }

    /**
     * 删除样品流转记录信息
     * 
     * @param id 样品流转记录ID
     * @return 结果
     */
    @Override
    public int deleteSampleCirculationBillById(String id)
    {
        return sampleCirculationBillMapper.deleteSampleCirculationBillById(id);
    }
}
